using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using TBFramework;
using System;
using System.Threading;
using StandardStorage;
using UnityEngine.UI;

using System.Threading.Tasks;
using JetBrains.Annotations;

//[ExecuteAlways]
public class ScriptRun : LocalMethod
{
    RunningMethod stepMethod;
    StepProperty stepProperty;

    Coroutine coro;
    // public delegate void kk();
    // public kk tt;
    // LinkedList<IEnumerator> linkedList = new LinkedList<IEnumerator>();
    public Text text;
    public string _testString;
    public string testString
    {
        get
        {
            return _testString;
        }
        set
        {
            print("testString set to: " + value);
        }
    }
    public Action<string> onEEGStatusChanged;

    public void RegisterESense(Action<string> statusChangedCallback)
    {

        onEEGStatusChanged += statusChangedCallback;
    }
    IECollection<Func<IEnumerator>> ieCollection = new IECollection<Func<IEnumerator>>(5);
    IECollection<RunClass> RunningMethodClass = new IECollection<RunClass>(5);
    public Transform target;
    async Task Start()
    {

        

        //sprites = ReturnResourcesSprites("pic");

        //文字特效
        //string tmpText = text.text;
        //TypeTextWithRichText(text, tmpText, 10f);
        //DOTween.To(() => string.Empty, value => text.text = value, tmpText, 50f).SetEase(Ease.Linear);

        //     RegisterESense(value=>testString = value);
        //     onEEGStatusChanged?.Invoke("EEG Status Changed: " + testString);
        //     print(testString);
        // Tang<GameObject> tang = new Tang<GameObject>
        // { 
        //     this.gameObject    
        // };

        //     foreach (var t in tang)
        //     {
        //         print(t);
        //     }

        //     base.Init();
        //     // 直接使用当前实例
        //     ScriptRun scriptRun = this;

        //     tt = testDelegate;
        //     tt();
        //     MethodInfo vr = typeof(ScriptRun).GetMethod("testDelegate");
        //     // 确保使用正确的实例创建委托
        //   //  print(Delegate.CreateDelegate(typeof(kk), vr));
        //   //  kk dl1 = (kk)Delegate.CreateDelegate(typeof(kk), vr);
        //   //  dl1();

        //     kk dl = (kk)Delegate.CreateDelegate(typeof(kk), scriptRun, "testDelegate");
        //     dl();

        //关闭携程结束执行 stepMethod.StopAll(coro);
        //     MethodCollection<Transform> methodCollection = new MethodCollection<Transform>(5);
        //     methodCollection[0, "dd"] = (Transform T) =>
        //     {
        //         print("sdfsdf");
        //     };
        //     if (methodCollection["dd"] != null)
        //     {
        //         methodCollection["dd"](this.transform);
        //     }
        //     //StartCoroutine(Trea.setup1("ddddd"));
        //    Trea trea = new Trea();
        //    StartCoroutine( (IEnumerator)trea.GetType().GetMethod("setup1").Invoke(null, new object[] { "ddddd111111" }));
        //    StartCoroutine( (IEnumerator)typeof(Trea).GetMethod("setup1").Invoke(null, new object[] { "ddddd111111111" }));


        // AsyncMethodCollection<string> asyncMethodCollection = new AsyncMethodCollection<string>(5);
        // asyncMethodCollection["test"] = async (param) =>
        // {
        //     // 调用 ExecuteAsync 方法并传入参数
        //     await ExecuteAsync(param);
        //     return "Operation completed";
        // };
        // await asyncMethodCollection["test"]("test");


        base.Init();
        stepProperty = gameObject.GetComponent<StepProperty>();
        stepMethod = gameObject.AddComponent<RunningMethod>();
        stepMethod.Init(stepProperty);


        stepMethod.StopAll(coro);
        stepProperty.DefaultValue = Default.LEVEL1;


        RunningMethodClass["物体1"] = new SetObjectTransform((target,true,true));

        ieCollection["提示方法"] = () => stepMethod.PromptInformationIE("提示信息",2,true,5f,false);


        ieCollection["添加播放视频方法"] = () => stepMethod.PlayMovie(stepProperty.stepCurrentProperty.movie);
        ieCollection["播放图片轮播"] = () => (IEnumerator)stepMethod.GetType().GetMethod("DisplayPictures")?.Invoke(stepMethod, new object[] {stepProperty.stepCurrentProperty.carouselImageObj.CarouselImageUIParent, stepProperty.stepCurrentProperty.stepCurrentProperty.carouselImage[0]}); // stepMethod.DisplayPictures(stepProperty.stepCurrentProperty.stepCurrentProperty.carouselImage);
        ieCollection["使用工具"] = () => stepMethod.ToolsUse(stepProperty.stepCurrentProperty.tools, stepProperty.stepCurrentProperty.stepCurrentProperty.toolsChangeValue[0]);
        ieCollection["播放图片轮播1"] = () => (IEnumerator)stepMethod.GetType().GetMethod("DisplayPictures")?.Invoke(stepMethod, new object[] {stepProperty.stepCurrentProperty.carouselImageObj.CarouselImageUIParent, stepProperty.stepCurrentProperty.stepCurrentProperty.carouselImage[1]});
        ieCollection["问答题"] = () => stepMethod.MultipleChoiceQuestion(stepProperty.stepCurrentProperty.multipleChoiceQuestionObj.singleChoiceQuestionUIParent,stepProperty.stepCurrentProperty.stepCurrentProperty.multipleChoiceQuestion[0]);
        ieCollection["问答题1"] = () => stepMethod.MultipleChoiceQuestion(stepProperty.stepCurrentProperty.multipleChoiceQuestionObj.singleChoiceQuestionUIParent,stepProperty.stepCurrentProperty.stepCurrentProperty.multipleChoiceQuestion[1]);
        ieCollection["问答题2"] = () => stepMethod.MultipleChoiceQuestion(stepProperty.stepCurrentProperty.multipleChoiceQuestionObj.singleChoiceQuestionUIParent,stepProperty.stepCurrentProperty.stepCurrentProperty.multipleChoiceQuestion[2]);

        ieCollection["选项变更图片"] = () => stepMethod.PictureOptions(stepProperty.stepCurrentProperty.pictureOptionsObj.PictureOptionsObjUIParent,stepProperty.stepCurrentProperty.stepCurrentProperty.pictureOptions[0]);
        StandardProperty.Instance.step = new List<IEnumerator>
        {
              ieCollection["提示方法"].Invoke(),
              ieCollection["问答题"].Invoke(),
              ieCollection["问答题1"].Invoke(),
              ieCollection["问答题2"].Invoke(),
              ieCollection["添加播放视频方法"].Invoke(),
              ieCollection["播放图片轮播"].Invoke(),
              ieCollection["使用工具"].Invoke(),
              ieCollection["播放图片轮播1"].Invoke(),
              ieCollection["选项变更图片"].Invoke()
        };
        print(ieCollection.Length);
        coro = StartCoroutine(standardClass.StartCoroutineMethod());
    }

    private void OnGUI() {

        if (GUILayout.Button("DDDD"))
        {
            RunningMethodClass["物体1"].ResetTransforms();
        }
    }

    private CancellationTokenSource cancellationTokenSource;
    public int i = 0;
    public void OnButton()
    {

        // 取消之前的异步操作
        if (cancellationTokenSource != null)
        {
            cancellationTokenSource.Cancel();
            cancellationTokenSource.Dispose();
        }
        standardClass.AsyncQueue = null;
        standardClass.DestoryDotween();
        cancellationTokenSource = new CancellationTokenSource();
        if (i % 2 == 0)
        {
            base.ChangeMinMaxWin(true);
            // 传递CancellationToken给异步方法
            standardClass.AddToAsyncQueue(standardClass.ExecuteAsyncMethod(standardClass, TestStatus.MOVEANDROTATE, cancellationTokenSource.Token, StandardProperty.Instance.cameraStruct.mainCamera.transform, new Vector3(0, 30, 0), new Vector3(0, 90, 0), 2f));
            standardClass.AddToAsyncQueue(standardClass.ExecuteAsyncMethod(standardClass, TestStatus.MOVEANDROTATE, cancellationTokenSource.Token, StandardProperty.Instance.cameraStruct.mainCamera.transform, new Vector3(0, 0, 0), new Vector3(0, 0, 0), 2f));
            standardClass.ActionMothed(() => { Debug.Log("sdfsdf"); });
        }
        else
        {
            base.ChangeMinMaxWin(false);
            standardClass.AddToAsyncQueue(standardClass.ExecuteAsyncMethod(standardClass, TestStatus.MOVEANDROTATE, cancellationTokenSource.Token, StandardProperty.Instance.cameraStruct.mainCamera.transform, new Vector3(0, 30, 0), new Vector3(0, 45, 0), 2f));
            standardClass.AddToAsyncQueue(standardClass.ExecuteAsyncMethod(standardClass, TestStatus.MOVEANDROTATE, cancellationTokenSource.Token, StandardProperty.Instance.cameraStruct.mainCamera.transform, new Vector3(0, 0, 0), new Vector3(0, 0, 0), 2f));
            standardClass.ActionMothed(() => { Debug.Log("sdfsdf"); });
        }
        i++;
    }

    void Update()
    {
        foreach (var item in StandardProperty.Instance.RefreshActions)
        {
            item?.Invoke();
        }
    }

    public void OnButton2()
    {
        stepMethod.StopAll(coro);
        stepProperty.DefaultValue = Default.LEVEL2;
        StandardProperty.Instance.step = new List<IEnumerator> {
            ieCollection["添加播放视频方法"].Invoke(),
            ieCollection["播放图片轮播"].Invoke(),
            ieCollection["添加播放视频方法"].Invoke(),
        };
        coro = StartCoroutine(standardClass.StartCoroutineMethod());
    }
}